<?php


class MessageManager {


    public function __construct(Mypdo $db){
        $this->db= $db;
    }

    public function add(Message $msg){
        $req = $this->db->prepare('INSERT INTO message ( id_propri, email_expe_msg, comptenu_msg ) VALUES
                  ( :id_propri, :email_expe_msg, :comptenu_msg )');
        $req->bindValue(':id_propri', $msg->getIdPropri(), PDO::PARAM_INT);
        $req->bindValue(':email_expe_msg', $msg->getEmailExpeMsg(), PDO::PARAM_STR);
        $req->bindValue(':comptenu_msg', $msg->getComptenuMsg(), PDO::PARAM_STR);

        $req->execute();

    }


    public function getList(){
        $listeMessages = array();
        $sql = 'SELECT * FROM message ORDER BY id_propri';
        $req = $this->db->query($sql);
        while ($message = $req->fetch(PDO::FETCH_OBJ)){
            $listeMessages[] = new Message($message);
        }
        $req->closeCursor();
        return $listeMessages;

    }

    public function  delMsgByIdPropri($id_propri){
        $sql = 'DELETE FROM immoloc.message WHERE id_propri = :id_propri';
        $req = $this->db->prepare($sql);
        $req->bindValue(':id_propri', $id_propri, PDO::PARAM_INT);

        $req->execute();

    }

    public function sendMsg(Message $msg, $id_loc)
    {
        $locManager = new LocationManager($this->db);
        $propriManager = new ProprietaireManager($this->db);
        $loc = $locManager->getLoc($id_loc);
        $propri = $propriManager->getPropri($msg->getIdPropri());
        $to      = $propri->getEmailPropri();
        $subject = 'Demande de location';
        $message = 'Bonjour,'."\r\n".
            'L\'adresse '.$msg->getEmailExpeMsg().' vous a envoyez un message à propos d\'une location : '."\r\n".
            $loc->getNomLoc().'-'.$loc->getMontantLoc().'euros-'.$loc->getSurfaceLoc().'m²'."\r\n".
            'Le Message :'."\r\n".
            $msg->getComptenuMsg();
        $headers = 'From: alexis-berger@orange.fr' . "\r\n" .
            'X-Mailer: PHP/' . phpversion();

        mail($to, $subject, $message, $headers);
    }
    public function reSendMsg(Message $msg)
    {
        $propriManager = new ProprietaireManager($this->db);
        $propri = $propriManager->getPropri($msg->getIdPropri());
        $to      = $propri->getEmailPropri();
        $subject = 'Demande de location';
        $message = $msg->getComptenuMsg();
        $headers = 'From: alexis-berger@orange.fr' . "\r\n" .
            'X-Mailer: PHP/' . phpversion();

        mail($to, $subject, $message, $headers);
    }

    public function getMsg($id_msg)
    {
        $sql = 'SELECT * FROM immoloc.message WHERE id_msg = :id_msg';
        $req = $this->db->prepare($sql);
        $req->bindValue(':id_msg', $id_msg, PDO::PARAM_INT);
        $req->execute();
        $liste = $req->fetch(PDO::FETCH_ASSOC);
        $msg = new Message($liste);

        return $msg;
    }

    public function delMsg($id_msg)
    {
        $sql = 'DELETE FROM immoloc.message WHERE id_msg = :id_msg';
        $req = $this->db->prepare($sql);
        $req->bindValue(':id_msg', $id_msg, PDO::PARAM_INT);

        $req->execute();
    }

} 